<template>
    <div style="width: 250px;
      height: 200px;
      margin: auto ;
      margin-top: 65%;
      ">

      <el-input
        placeholder="请输入账号"
        prefix-icon="el-icon-search"
        v-model="input1">
      </el-input>

      <el-input
        style="margin-top: 20px;"
        placeholder="请输入密码"
        prefix-icon="el-icon-search"
        v-model="input2">
      </el-input>
      
      <router-link to="/enroll">
        <el-button
          style="margin-top: 15%;"
          type="primary">注册
        </el-button>
      </router-link>

      <el-button
        @click="denglu"
        style="margin-left: 44%;"
        type="primary">登录
      </el-button>
    </div>
 
</template>

<script>

import axios from 'axios'
export default {
    
    name:'Login',
    data() {
      return {
        input1:'',
        input2:'',
      }
    },
    methods:{
      denglu(){
        const options = {
          method: 'GET',
          headers: {
            "Content-Type": 'application/json'
          }
        }
        fetch(`http://8.130.95.37:8085/getUser/${this.input1}/${this.input2}`, options).then(res => res.json()).then(res => {
          if(res[0] != null){
            this.$message({
              message: '恭喜你，登录成功！',
              type: 'success'
            });
            this.$store.state.udate=res[0]
            this.$router.push('/home/list')
            localStorage.setItem("name", this.$store.state.udate.uid);
            
          }else{
            this.$message.error('账号密码错误！');
            this.$router.push('/')
          }
        }).catch(err => {
          console.log(err)
        })
        
      }
    },
    mounted(){
      if (localStorage.getItem("name")!=null) {
        const options = {
          method: 'GET',
          headers: {
            "Content-Type": 'application/json'
          }
        }
        fetch(`http://8.130.95.37:8085/getUser2/${localStorage.getItem("name")}`, options).then(res => res.json()).then(res => {
          this.$store.state.udate=res[0]
          this.$router.push('/home/list')
        }).catch(err => {
          console.log(err)
        })
      }
      
    }

    
}
</script>


<style scoped>

</style>
